<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0,viewport-fit=cover"><title>分类: 教程 | 小小程序员</title><meta name="author" content="十一星野"><meta name="copyright" content="十一星野"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="Redis 的数据类型以及使用场景，看这一篇就够啦原创 2021-06-20 18:02· 猿视野 Redis 数据类型 string String hash HashMap list LinkedList set HashSet sorted_set TreeSet  redis 数据存储格式redis 自身是一个 Map，其中所有的数据都是采用 key : value 的形式存储 数据类型指的">
<meta property="og:type" content="article">
<meta property="og:title" content="Redis的数据类型以及使用场景，看这一篇就够啦">
<meta property="og:url" content="https://ko25891wan.gitlab.io/2023/12/dd803e04aee5.html">
<meta property="og:site_name" content="小小程序员">
<meta property="og:description" content="Redis 的数据类型以及使用场景，看这一篇就够啦原创 2021-06-20 18:02· 猿视野 Redis 数据类型 string String hash HashMap list LinkedList set HashSet sorted_set TreeSet  redis 数据存储格式redis 自身是一个 Map，其中所有的数据都是采用 key : value 的形式存储 数据类型指的">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/%E5%A4%B4%E5%83%8F/%E7%81%B0%E5%A4%AA%E7%8B%BC.png">
<meta property="article:published_time" content="2023-12-10T07:20:29.000Z">
<meta property="article:modified_time" content="2024-02-02T09:02:26.476Z">
<meta property="article:author" content="十一星野">
<meta property="article:tag" content="redis">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/%E5%A4%B4%E5%83%8F/%E7%81%B0%E5%A4%AA%E7%8B%BC.png"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="https://ko25891wan.gitlab.io/2023/12/dd803e04aee5.html"><link rel="preconnect" href="//fastly.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.5.1/css/all.min.css"><link rel="stylesheet" href="https://cdn.staticfile.org/fancyapps-ui/5.0.32/fancybox/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = {
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.xml","preload":false,"top_n_per_article":1,"unescape":false,"languages":{"hits_empty":"找不到您查询的内容：${query}","hits_stats":"共找到 ${hits} 篇文章"}},
  translate: {"defaultEncoding":1,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"简"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlight.js","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":200},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: true,
    post: true
  },
  runtime: '',
  dateSuffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  infinitegrid: {
    js: 'https://cdn.staticfile.org/egjs-infinitegrid/4.11.0/infinitegrid.min.js',
    buttonText: '加载更多'
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: true,
  },
  autoDarkmode: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '分类: 教程',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2024-02-02 17:02:26'
}</script><script>(win=>{
      win.saveToLocal = {
        set: (key, value, ttl) => {
          if (ttl === 0) return
          const now = Date.now()
          const expiry = now + ttl * 86400000
          const item = {
            value,
            expiry
          }
          localStorage.setItem(key, JSON.stringify(item))
        },
      
        get: key => {
          const itemStr = localStorage.getItem(key)
      
          if (!itemStr) {
            return undefined
          }
          const item = JSON.parse(itemStr)
          const now = Date.now()
      
          if (now > item.expiry) {
            localStorage.removeItem(key)
            return undefined
          }
          return item.value
        }
      }
    
      win.getScript = (url, attr = {}) => new Promise((resolve, reject) => {
        const script = document.createElement('script')
        script.src = url
        script.async = true
        script.onerror = reject
        script.onload = script.onreadystatechange = function() {
          const loadState = this.readyState
          if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
          script.onload = script.onreadystatechange = null
          resolve()
        }

        Object.keys(attr).forEach(key => {
          script.setAttribute(key, attr[key])
        })

        document.head.appendChild(script)
      })
    
      win.getCSS = (url, id = false) => new Promise((resolve, reject) => {
        const link = document.createElement('link')
        link.rel = 'stylesheet'
        link.href = url
        if (id) link.id = id
        link.onerror = reject
        link.onload = link.onreadystatechange = function() {
          const loadState = this.readyState
          if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
          link.onload = link.onreadystatechange = null
          resolve()
        }
        document.head.appendChild(link)
      })
    
      win.activateDarkMode = () => {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = () => {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
        if (t === 'dark') activateDarkMode()
        else if (t === 'light') activateLightMode()
      
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
      const detectApple = () => {
        if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
          document.documentElement.classList.add('apple')
        }
      }
      detectApple()
    })(window)</script><meta name="generator" content="Hexo 6.3.0"></head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><script>(()=>{
  const $loadingBox = document.getElementById('loading-box')
  const $body = document.body
  const preloader = {
    endLoading: () => {
      $body.style.overflow = ''
      $loadingBox.classList.add('loaded')
    },
    initLoading: () => {
      $body.style.overflow = 'hidden'
      $loadingBox.classList.remove('loaded')
    }
  }

  preloader.initLoading()
  window.addEventListener('load',() => { preloader.endLoading() })

  if (false) {
    document.addEventListener('pjax:send', () => { preloader.initLoading() })
    document.addEventListener('pjax:complete', () => { preloader.endLoading() })
  }
})()</script><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/%E5%A4%B4%E5%83%8F/%E7%81%B0%E5%A4%AA%E7%8B%BC.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">120</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">4</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">22</div></a></div><hr class="custom-hr"/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-heartbeat"></i><span> 清单</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/Gallery/"><i class="fa-fw fas fa-images"></i><span> 照片</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-tools"></i><span> 工具</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/md_editor/"><i class="fa-fw fas fa-pen"></i><span> MDEditor_my</span></a></li></ul></div></div></div></div><div class="post" id="body-wrap"><header class="not-top-img" id="page-header"><nav id="nav"><span id="blog-info"><a href="/" title="小小程序员"><span class="site-name">小小程序员</span></a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search" href="javascript:void(0);"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-heartbeat"></i><span> 清单</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/Gallery/"><i class="fa-fw fas fa-images"></i><span> 照片</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-tools"></i><span> 工具</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/md_editor/"><i class="fa-fw fas fa-pen"></i><span> MDEditor_my</span></a></li></ul></div></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);"><i class="fas fa-bars fa-fw"></i></a></div></div></nav></header><main class="layout" id="content-inner"><div id="post"><div id="post-info"><h1 class="post-title">Redis的数据类型以及使用场景，看这一篇就够啦</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2023-12-10T07:20:29.000Z" title="发表于 2023-12-10 15:20:29">2023-12-10</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2024-02-02T09:02:26.476Z" title="更新于 2024-02-02 17:02:26">2024-02-02</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E6%95%99%E7%A8%8B/">教程</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">5.4k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>25分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="Redis的数据类型以及使用场景，看这一篇就够啦"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div><article class="post-content" id="article-container"><h1 id="Redis-的数据类型以及使用场景，看这一篇就够啦"><a href="#Redis-的数据类型以及使用场景，看这一篇就够啦" class="headerlink" title="Redis 的数据类型以及使用场景，看这一篇就够啦"></a>Redis 的数据类型以及使用场景，看这一篇就够啦</h1><p>原创 2021-06-20 18:02· <a href="/c/user/token/MS4wLjABAAAA4paGI23ABjZ1Rnq1x17cUKzEhj9h2AnS9WiyEXj18C4/?source=tuwen_detail">猿视野</a></p>
<h1 id="Redis-数据类型"><a href="#Redis-数据类型" class="headerlink" title="Redis 数据类型"></a>Redis 数据类型</h1><ul>
<li>string String</li>
<li>hash HashMap</li>
<li>list LinkedList</li>
<li>set HashSet</li>
<li>sorted_set TreeSet</li>
</ul>
<h1 id="redis-数据存储格式"><a href="#redis-数据存储格式" class="headerlink" title="redis 数据存储格式"></a>redis 数据存储格式</h1><p>redis 自身是一个 Map，其中所有的数据都是采用 key : value 的形式存储</p>
<p>数据类型指的是存储的数据的类型，也就是 value 部分的类型，key 部分永远都是字符串<br><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/obsition/20240202153022.png"></p>
<h1 id="string-类型"><a href="#string-类型" class="headerlink" title="string 类型"></a>string 类型</h1><p><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/obsition/20240202153046.png"></p>
<h1 id="string-操作"><a href="#string-操作" class="headerlink" title="string 操作"></a>string 操作</h1><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br></pre></td><td class="code"><pre><span class="line">存储的数据：单个数据，最简单的数据存储类型，也是最常用的数据存储类型</span><br><span class="line">存储数据的格式：一个存储空间保存一个数据</span><br><span class="line">存储内容：通常使用字符串，如果字符串以整数的形式展示，可以作为数字操作使用</span><br><span class="line"></span><br><span class="line">添加/修改数据</span><br><span class="line">set key value</span><br><span class="line">获取数据</span><br><span class="line">get key</span><br><span class="line">删除数据</span><br><span class="line">del key</span><br><span class="line"></span><br><span class="line">127.0.0.1:6379&gt; set age 100</span><br><span class="line">OK</span><br><span class="line">127.0.0.1:6379&gt; get age</span><br><span class="line">&quot;100&quot;</span><br><span class="line">127.0.0.1:6379&gt; del age</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; del age</span><br><span class="line">(integer) 0</span><br><span class="line"></span><br><span class="line">添加/修改多个数据</span><br><span class="line">mset key1 value1 key2 value2 …</span><br><span class="line">获取多个数据</span><br><span class="line">mget key1 key2 …</span><br><span class="line">获取数据字符个数（字符串长度）</span><br><span class="line">strlen key</span><br><span class="line">追加信息到原始信息后部（如果原始信息存在就追加，否则新建）</span><br><span class="line">append key value</span><br><span class="line">127.0.0.1:6379&gt; mset set a 1 b 2 c 3</span><br><span class="line">(error) ERR wrong number of arguments for MSET</span><br><span class="line">127.0.0.1:6379&gt; mset a 1 b 2 c 3</span><br><span class="line">OK</span><br><span class="line">127.0.0.1:6379&gt; mget a b c</span><br><span class="line">1) &quot;1&quot;</span><br><span class="line">2) &quot;2&quot;</span><br><span class="line">3) &quot;3&quot;</span><br><span class="line">127.0.0.1:6379&gt; strlen a</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; append a 23</span><br><span class="line">(integer) 3</span><br><span class="line">127.0.0.1:6379&gt; get a</span><br><span class="line">&quot;123&quot;</span><br><span class="line"></span><br><span class="line">设置数值数据增加指定范围的值</span><br><span class="line">incr key  # 增加1</span><br><span class="line">incrby key increment</span><br><span class="line">incrbyfloat key increment</span><br><span class="line"></span><br><span class="line">127.0.0.1:6379&gt; get a</span><br><span class="line">&quot;123&quot;</span><br><span class="line">127.0.0.1:6379&gt; incr a</span><br><span class="line">(integer) 124</span><br><span class="line">127.0.0.1:6379&gt; incrby a 26</span><br><span class="line">(integer) 150</span><br><span class="line">127.0.0.1:6379&gt; incrbyfloat a 50</span><br><span class="line">&quot;200&quot;</span><br><span class="line"></span><br><span class="line">设置数值数据减少指定范围的值</span><br><span class="line">decr key # 减少1</span><br><span class="line">decrby key increment</span><br><span class="line"></span><br><span class="line">127.0.0.1:6379&gt; get a</span><br><span class="line">&quot;174&quot;</span><br><span class="line">127.0.0.1:6379&gt; decr a</span><br><span class="line">(integer) 173</span><br><span class="line">127.0.0.1:6379&gt; decrby a 25</span><br><span class="line">(integer) 148</span><br><span class="line"></span><br><span class="line">string 作为数值操作</span><br><span class="line">string在redis内部存储默认就是一个字符串，当遇到增减类操作incr，decr时会转成数值型进行计算。</span><br><span class="line">redis所有的操作都是原子性的，采用单线程处理所有业务，命令是一个一个执行的，因此无需考虑并发</span><br><span class="line">带来的数据影响。</span><br><span class="line">注意：按数值进行操作的数据，如果原始数据不能转成数值，或超越了redis 数值上限范围，将报错。</span><br><span class="line"></span><br><span class="line">9223372036854775807（java中long型数据最大值，Long.MAX_VALUE）</span><br><span class="line"></span><br><span class="line">Tips 1：</span><br><span class="line">redis用于控制数据库表主键id，为数据库表主键提供生成策略，保障数据库表的主键唯一性</span><br><span class="line">此方案适用于所有数据库，且支持数据库集群</span><br><span class="line"></span><br><span class="line">设置数据具有指定的生命周期</span><br><span class="line">setex key seconds value  # 秒为单位</span><br><span class="line">psetex key milliseconds value   # 毫秒为单位</span><br><span class="line"></span><br><span class="line">Tips 2：</span><br><span class="line">redis 控制数据的生命周期，通过数据是否失效控制业务行为，适用于所有具有时效性限定控制的操作</span><br><span class="line"></span><br><span class="line">127.0.0.1:6379&gt; setex a 5 1</span><br><span class="line">OK</span><br><span class="line">127.0.0.1:6379&gt; get a</span><br><span class="line">&quot;1&quot;</span><br><span class="line">127.0.0.1:6379&gt; get a</span><br><span class="line">(nil)</span><br><span class="line"></span><br><span class="line">127.0.0.1:6379&gt; psetex a 5000 2</span><br><span class="line">OK</span><br><span class="line">127.0.0.1:6379&gt; get a</span><br><span class="line">&quot;2&quot;</span><br><span class="line">127.0.0.1:6379&gt; get a</span><br><span class="line">&quot;2&quot;</span><br><span class="line">127.0.0.1:6379&gt; get a</span><br><span class="line">(nil)</span><br><span class="line"></span><br><span class="line">string 类型数据操作的注意事项</span><br><span class="line">数据操作不成功的反馈与数据正常操作之间的差异</span><br><span class="line">① 表示运行结果是否成功</span><br><span class="line">(integer) 0 → false 失败</span><br><span class="line">(integer) 1 → true 成功</span><br><span class="line">② 表示运行结果值</span><br><span class="line">(integer) 3 → 3 3个</span><br><span class="line">(integer) 1 → 1 1个</span><br><span class="line">数据未获取到</span><br><span class="line">（nil）等同于null</span><br><span class="line">数据最大存储量</span><br><span class="line">512MB</span><br><span class="line">数值计算最大范围（java中的long的最大值）</span><br><span class="line">9223372036854775807 </span><br></pre></td></tr></table></figure>

<h1 id="string-类型应用场景"><a href="#string-类型应用场景" class="headerlink" title="string 类型应用场景"></a>string 类型应用场景</h1><p><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/obsition/20240202153058.png"></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">在redis中为大V用户设定用户信息，以用户主键和属性值作为key，后台设定定时刷新策略即可</span><br><span class="line">eg: user:id:3506728370:fans → 12210947</span><br><span class="line">eg: user:id:3506728370:blogs → 6164</span><br><span class="line">eg: user:id:3506728370:focuss → 83</span><br><span class="line"></span><br><span class="line">127.0.0.1:6379&gt; set user:id:3506728370:fans 12210947</span><br><span class="line">OK</span><br><span class="line">127.0.0.1:6379&gt; get user:id:3506728370:fans </span><br><span class="line">&quot;12210947&quot;</span><br><span class="line">127.0.0.1:6379&gt; set user:id:3506728370:blogs 6164</span><br><span class="line">OK</span><br><span class="line">get user:id:3506728370:blogs </span><br><span class="line">&quot;6164&quot;</span><br><span class="line">127.0.0.1:6379&gt; set user:id:3506728370:focuss 83</span><br><span class="line">OK</span><br><span class="line">127.0.0.1:6379&gt; get user:id:3506728370:focuss </span><br><span class="line">&quot;83&quot;</span><br><span class="line"></span><br><span class="line">在redis中以json格式存储大V用户信息，定时刷新（也可以使用hash类型）</span><br><span class="line">eg: user:id:3506728370 →</span><br><span class="line">&#123;&quot;id&quot;:3506728370,&quot;name&quot;:&quot;春晚&quot;,&quot;fans&quot;:12210862,&quot;blogs&quot;:6164, &quot;focus&quot;:83&#125;</span><br><span class="line"></span><br><span class="line">Tips 3：</span><br><span class="line">redis应用于各种结构型和非结构型高热度数据访问加速</span><br></pre></td></tr></table></figure>

<h1 id="hash"><a href="#hash" class="headerlink" title="hash"></a>hash</h1><h1 id="hash-操作"><a href="#hash-操作" class="headerlink" title="hash 操作"></a>hash 操作</h1><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br></pre></td><td class="code"><pre><span class="line">添加/修改数据</span><br><span class="line">hset key field value</span><br><span class="line">获取数据</span><br><span class="line">hget key field</span><br><span class="line">hgetall key</span><br><span class="line">删除数据</span><br><span class="line">hdel key field1 [field2]</span><br><span class="line"></span><br><span class="line">127.0.0.1:6379&gt; hset a name xiaoming</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; hset a age 18</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; hget a name</span><br><span class="line">&quot;xiaoming&quot;</span><br><span class="line">127.0.0.1:6379&gt; hgetall a</span><br><span class="line">1) &quot;name&quot;</span><br><span class="line">2) &quot;xiaoming&quot;</span><br><span class="line">3) &quot;age&quot;</span><br><span class="line">4) &quot;18&quot;</span><br><span class="line">127.0.0.1:6379&gt; hdel a name</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; hgetall a</span><br><span class="line">1) &quot;age&quot;</span><br><span class="line">2) &quot;18&quot;</span><br><span class="line"></span><br><span class="line">添加/修改多个数据</span><br><span class="line">hmset key field1 valuel field2 value2</span><br><span class="line">获取多个数据</span><br><span class="line">hmget key fieldl field2</span><br><span class="line">获取哈希表中字段的数量</span><br><span class="line">hlen key</span><br><span class="line">获取哈希表中是否存在指定的字段</span><br><span class="line">exists key field</span><br><span class="line"></span><br><span class="line">127.0.0.1:6379&gt; hmset a name xiaohong age 20</span><br><span class="line">OK</span><br><span class="line">127.0.0.1:6379&gt; hmget a name age</span><br><span class="line">1) &quot;xiaohong&quot;</span><br><span class="line">2) &quot;20&quot;</span><br><span class="line">127.0.0.1:6379&gt; hlen a</span><br><span class="line">(integer) 2</span><br><span class="line">127.0.0.1:6379&gt; exists a name</span><br><span class="line">(integer) 1</span><br><span class="line"></span><br><span class="line">获取哈希表中所有的字段名或字段值</span><br><span class="line">keys key</span><br><span class="line">hals key</span><br><span class="line"></span><br><span class="line">127.0.0.1:6379&gt; hkeys a</span><br><span class="line">1) &quot;age&quot;</span><br><span class="line">2) &quot;name&quot;</span><br><span class="line">127.0.0.1:6379&gt; hvals a</span><br><span class="line">1) &quot;20&quot;</span><br><span class="line">2) &quot;xiaohong&quot;</span><br><span class="line"></span><br><span class="line">设置指定字段的数值数据増加指定范围的值</span><br><span class="line">hincrby key field increment</span><br><span class="line">hincrbyfloat key field increment</span><br><span class="line"></span><br><span class="line">127.0.0.1:6379&gt; hincrby a age 10</span><br><span class="line">(integer) 30</span><br><span class="line">127.0.0.1:6379&gt; hgetall a</span><br><span class="line">1) &quot;age&quot;</span><br><span class="line">2) &quot;30&quot;</span><br><span class="line">3) &quot;name&quot;</span><br><span class="line">4) &quot;xiaohong&quot;</span><br><span class="line">127.0.0.1:6379&gt; hincrbyfloat a age 30.1</span><br><span class="line">&quot;60.1&quot;</span><br><span class="line">127.0.0.1:6379&gt; hgetall a</span><br><span class="line">1) &quot;age&quot;</span><br><span class="line">2) &quot;60.1&quot;</span><br><span class="line">3) &quot;name&quot;</span><br><span class="line">4) &quot;xiaohong&quot;</span><br><span class="line"></span><br><span class="line">hash类型数据操作的注意事项</span><br><span class="line">hash类型下的 value只能存储字符串，不允许存储其他数据类型，不存在嵌套现象。如果数据未获取到,对应的值为（nil）</span><br><span class="line">每个hash可以存储2^32-1个键值对</span><br><span class="line">hash类型十分贴近对象的数据存储形式，并且可以灵活添加删除对象属性。但hash设计初衷不是为了存储大量对象而设计的，切记不可滥用，更不可以将hash作为对象列表使用</span><br><span class="line">hgetal操作可以获取全部属性，如果内部 field过多，遍历整体数据效率就会很低，有可能成为数据访问瓶颈</span><br></pre></td></tr></table></figure>

<h1 id="Hash-类型应用场景"><a href="#Hash-类型应用场景" class="headerlink" title="Hash 类型应用场景"></a>Hash 类型应用场景</h1><h1 id="电商网站购物车设计与实现"><a href="#电商网站购物车设计与实现" class="headerlink" title="电商网站购物车设计与实现"></a>电商网站购物车设计与实现</h1><p><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/obsition/20240202153111.png"></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line">解决方案</span><br><span class="line">以客户id作为key，每位客户创建一个hash存储结构存储对应的购物车信息</span><br><span class="line">将商品编号作为field，购买数量作为value进行存储</span><br><span class="line">添加商品：追加全新的field与value</span><br><span class="line">浏览：遍历hash</span><br><span class="line">更改数量：自增/自减，设置value值</span><br><span class="line">删除商品：删除field</span><br><span class="line">清空：删除key</span><br><span class="line">此处仅讨论购物车中的模型设计</span><br><span class="line">购物车与数据库间持久化同步、购物车与订单间关系、未登录用户购物车信息存储不进行讨论</span><br><span class="line"></span><br><span class="line">127.0.0.1:6379&gt; hmset 001 g01 100 g02 200</span><br><span class="line">OK</span><br><span class="line">127.0.0.1:6379&gt; hmset 002 g02 1 g04 7 g05 100</span><br><span class="line">OK</span><br><span class="line">127.0.0.1:6379&gt; hset 001 g03 5</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; hgetall 001</span><br><span class="line">1) &quot;g01&quot;</span><br><span class="line">2) &quot;100&quot;</span><br><span class="line">3) &quot;g02&quot;</span><br><span class="line">4) &quot;200&quot;</span><br><span class="line">5) &quot;g03&quot;</span><br><span class="line">6) &quot;5&quot;</span><br><span class="line">127.0.0.1:6379&gt; hdel 001 g01</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; hgetall 001</span><br><span class="line">1) &quot;g02&quot;</span><br><span class="line">2) &quot;200&quot;</span><br><span class="line">3) &quot;g03&quot;</span><br><span class="line">4) &quot;5&quot;</span><br><span class="line">127.0.0.1:6379&gt; hincrby 001 g03 1</span><br><span class="line">(integer) 6</span><br><span class="line">127.0.0.1:6379&gt; hgetall 001</span><br><span class="line">1) &quot;g02&quot;</span><br><span class="line">2) &quot;200&quot;</span><br><span class="line">3) &quot;g03&quot;</span><br><span class="line">4) &quot;6&quot;</span><br></pre></td></tr></table></figure>

<h1 id="list"><a href="#list" class="headerlink" title="list"></a>list</h1><h1 id="list-操作"><a href="#list-操作" class="headerlink" title="list 操作"></a>list 操作</h1><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br></pre></td><td class="code"><pre><span class="line">数据存储需求：存储多个数据，并对数据进入存储空间的顺序进行区分</span><br><span class="line">需要的存储结构：一个存储空间保存多个数据，且通过数据可以体现进入顺序</span><br><span class="line">list类型：保存多个数据，底层使用双向链表存储结构实现</span><br><span class="line"></span><br><span class="line">添加/修改数据</span><br><span class="line">lpush key value1 [value2] ……</span><br><span class="line">rpush key value1 [value2] ……</span><br><span class="line"></span><br><span class="line">获取数据</span><br><span class="line">lrange key start stop</span><br><span class="line">lindex key index</span><br><span class="line">llen key </span><br><span class="line"></span><br><span class="line">获取并移除数据</span><br><span class="line">lpop key</span><br><span class="line">rpop key</span><br><span class="line"></span><br><span class="line">127.0.0.1:6379&gt; lpush list1 a b c</span><br><span class="line">(integer) 3</span><br><span class="line">127.0.0.1:6379&gt; rpush list2 d e f</span><br><span class="line">(integer) 3</span><br><span class="line">127.0.0.1:6379&gt; lrange list1 0 -1</span><br><span class="line">1) &quot;c&quot;</span><br><span class="line">2) &quot;b&quot;</span><br><span class="line">3) &quot;a&quot;</span><br><span class="line">127.0.0.1:6379&gt; lrange list2 0 -1</span><br><span class="line">1) &quot;d&quot;</span><br><span class="line">2) &quot;e&quot;</span><br><span class="line">3) &quot;f&quot;</span><br><span class="line">127.0.0.1:6379&gt; lindex list1 0</span><br><span class="line">&quot;c&quot;</span><br><span class="line">127.0.0.1:6379&gt; lindex list1 -1</span><br><span class="line">&quot;a&quot;</span><br><span class="line">127.0.0.1:6379&gt; lindex list1 -2</span><br><span class="line">&quot;b&quot;</span><br><span class="line">127.0.0.1:6379&gt; llen list1</span><br><span class="line">(integer) 3</span><br><span class="line">127.0.0.1:6379&gt; llen list2</span><br><span class="line">(integer) 3</span><br><span class="line">127.0.0.1:6379&gt; lpop list1</span><br><span class="line">&quot;c&quot;</span><br><span class="line">127.0.0.1:6379&gt; rpop list1</span><br><span class="line">&quot;a&quot;</span><br><span class="line">127.0.0.1:6379&gt; llen list1</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; lpop list2 </span><br><span class="line">&quot;d&quot;</span><br><span class="line">127.0.0.1:6379&gt; rpop list2</span><br><span class="line">&quot;f&quot;</span><br><span class="line">127.0.0.1:6379&gt; llen list2</span><br><span class="line">(integer) 1</span><br><span class="line"></span><br><span class="line">规定时间内获取并移除数据</span><br><span class="line">blpop key1 [key2] timeout</span><br><span class="line">brpop key1 [key2] timeout</span><br><span class="line"></span><br><span class="line">127.0.0.1:6379&gt; keys *</span><br><span class="line">1) &quot;list2&quot;</span><br><span class="line">127.0.0.1:6379&gt; lpush list1 aa</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; keys *</span><br><span class="line">1) &quot;list2&quot;</span><br><span class="line">127.0.0.1:6379&gt; lpush list3 bb</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; </span><br><span class="line"></span><br><span class="line">127.0.0.1:6379&gt; blpop list1 10</span><br><span class="line">1) &quot;list1&quot;</span><br><span class="line">2) &quot;aa&quot;</span><br><span class="line">127.0.0.1:6379&gt; blpop list3 10</span><br><span class="line">1) &quot;list3&quot;</span><br><span class="line">2) &quot;bb&quot;(3.78s)</span><br></pre></td></tr></table></figure>

<h1 id="List-类型应用场景"><a href="#List-类型应用场景" class="headerlink" title="List 类型应用场景"></a>List 类型应用场景</h1><p><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/obsition/20240202153128.png"></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">list 类型数据扩展操作</span><br><span class="line">移除指定数据</span><br><span class="line">lrem key count value  # count表示移除的个数</span><br><span class="line"></span><br><span class="line">Tips 6：</span><br><span class="line">redis 应用于具有操作先后顺序的数据控制</span><br><span class="line"></span><br><span class="line">127.0.0.1:6379&gt; lpush list1 a b c d a</span><br><span class="line">(integer) 5</span><br><span class="line">127.0.0.1:6379&gt; lrange list1 0 -1</span><br><span class="line">1) &quot;a&quot;</span><br><span class="line">2) &quot;d&quot;</span><br><span class="line">3) &quot;c&quot;</span><br><span class="line">4) &quot;b&quot;</span><br><span class="line">5) &quot;a&quot;</span><br><span class="line">127.0.0.1:6379&gt; lrem list1 2 a</span><br><span class="line">(integer) 2</span><br><span class="line">127.0.0.1:6379&gt; llen list1</span><br><span class="line">(integer) 3</span><br><span class="line">127.0.0.1:6379&gt; lrange list1 0 -1</span><br><span class="line">1) &quot;d&quot;</span><br><span class="line">2) &quot;c&quot;</span><br><span class="line">3) &quot;b&quot;</span><br></pre></td></tr></table></figure>

<h1 id="set-类型"><a href="#set-类型" class="headerlink" title="set 类型"></a>set 类型</h1><h1 id="set-操作"><a href="#set-操作" class="headerlink" title="set 操作"></a>set 操作</h1><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br></pre></td><td class="code"><pre><span class="line">新的存储需求：存储大量的数据，在查询方面提供更高的效率</span><br><span class="line">需要的存储结构：能够保存大量的数据，高效的内部存储机制，便于查询</span><br><span class="line">set类型：与hash存储结构完全相同，仅存储键，不存储值（nil），并且值是不允许重复的</span><br><span class="line"></span><br><span class="line">set 类型数据的基本操作</span><br><span class="line">添加数据</span><br><span class="line">sadd key member1 [member2]</span><br><span class="line">获取全部数据</span><br><span class="line">smembers key </span><br><span class="line">删除数据</span><br><span class="line">srem key member1 [member2]</span><br><span class="line">获取集合数据总量</span><br><span class="line">scard key</span><br><span class="line">判断集合中是否包含指定数据</span><br><span class="line">sismember key member</span><br><span class="line"></span><br><span class="line">127.0.0.1:6379&gt; sadd users zs</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; sadd users ls</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; sadd users ww</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; smembers users</span><br><span class="line">1) &quot;zs&quot;</span><br><span class="line">2) &quot;ww&quot;</span><br><span class="line">3) &quot;ls&quot;</span><br><span class="line">127.0.0.1:6379&gt; srem users ww</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; smembers users</span><br><span class="line">1) &quot;zs&quot;</span><br><span class="line">2) &quot;ls&quot;</span><br><span class="line">127.0.0.1:6379&gt; scard users</span><br><span class="line">(integer) 2</span><br><span class="line">127.0.0.1:6379&gt; sismember users zs</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; sismember users ww</span><br><span class="line">(integer) 0</span><br><span class="line"></span><br><span class="line">set 类型数据操作的注意事项</span><br><span class="line">set 类型不允许数据重复，如果添加的数据在 set 中已经存在，将只保留一份</span><br><span class="line">set 虽然与hash的存储结构相同，但是无法启用hash中存储值的空间</span><br><span class="line"></span><br><span class="line">127.0.0.1:6379&gt; sadd a a</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; sadd a a</span><br><span class="line">(integer) 0</span><br><span class="line">127.0.0.1:6379&gt; hset a a 1</span><br><span class="line">(error) WRONGTYPE Operation against a key holding the wrong kind of value</span><br><span class="line"></span><br><span class="line">set 类型数据的扩展操作</span><br><span class="line">随机获取集合中指定数量的数据</span><br><span class="line">srandmember key [count]</span><br><span class="line"></span><br><span class="line">随机获取集合中的某个数据并将该数据移出集合</span><br><span class="line">spop key [count]</span><br><span class="line"></span><br><span class="line">Tips 8：</span><br><span class="line">redis 应用于随机推荐类信息检索，例如热点歌单推荐，热点新闻推荐，热卖旅游线路，应用APP推荐，</span><br><span class="line">大V推荐等</span><br><span class="line"></span><br><span class="line">Tips 9：</span><br><span class="line">redis 应用于同类信息的关联搜索，二度关联搜索，深度关联搜索</span><br><span class="line">显示共同关注（一度）</span><br><span class="line">显示共同好友（一度）</span><br><span class="line">由用户A出发，获取到好友用户B的好友信息列表（一度）</span><br><span class="line">由用户A出发，获取到好友用户B的购物清单列表（二度）</span><br><span class="line">由用户A出发，获取到好友用户B的游戏充值列表（二度）</span><br><span class="line"></span><br><span class="line">127.0.0.1:6379&gt; sadd news n1</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; sadd news n2</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; sadd news n3</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; sadd news n4</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; srandmember news 1</span><br><span class="line">1) &quot;n1&quot;</span><br><span class="line">127.0.0.1:6379&gt; srandmember news 1</span><br><span class="line">1) &quot;n1&quot;</span><br><span class="line">127.0.0.1:6379&gt; srandmember news 1</span><br><span class="line">1) &quot;n1&quot;</span><br><span class="line">127.0.0.1:6379&gt; srandmember news 1</span><br><span class="line">1) &quot;n3&quot;</span><br><span class="line">127.0.0.1:6379&gt; scard news</span><br><span class="line">(integer) 4</span><br><span class="line">127.0.0.1:6379&gt; spop news 1</span><br><span class="line">1) &quot;n1&quot;</span><br><span class="line">127.0.0.1:6379&gt; smembers news</span><br><span class="line">1) &quot;n4&quot;</span><br><span class="line">2) &quot;n2&quot;</span><br><span class="line">3) &quot;n3&quot;</span><br><span class="line">127.0.0.1:6379&gt; spop news 2</span><br><span class="line">1) &quot;n4&quot;</span><br><span class="line">2) &quot;n3&quot;</span><br><span class="line">127.0.0.1:6379&gt; smembers news</span><br><span class="line">1) &quot;n2&quot;</span><br><span class="line"></span><br><span class="line">求两个集合的交、并、差集</span><br><span class="line">sinter key1 [key2]</span><br><span class="line">sunion key1 [key2]</span><br><span class="line">sdiff key1 [key2]</span><br><span class="line"></span><br><span class="line">求两个集合的交、并、差集并存储到指定集合中</span><br><span class="line">sinterstore destination key1 [key2]</span><br><span class="line">sunionstore destination key1 [key2]</span><br><span class="line">sdiffstore destination key1 [key2] </span><br><span class="line"></span><br><span class="line">将指定数据从原始集合中移动到目标集合中</span><br><span class="line">smove source destination member </span><br><span class="line"></span><br><span class="line">127.0.0.1:6379&gt; sadd u1 a1</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; sadd u1 s1</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; sadd u1 b1</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; sadd u2 s1</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; sadd u2 w1</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; sinter u1 u2</span><br><span class="line">1) &quot;s1&quot;</span><br><span class="line">127.0.0.1:6379&gt; sunion u1 u2</span><br><span class="line">1) &quot;a1&quot;</span><br><span class="line">2) &quot;s1&quot;</span><br><span class="line">3) &quot;w1&quot;</span><br><span class="line">4) &quot;b1&quot;</span><br><span class="line">127.0.0.1:6379&gt; sdiff u1 u2</span><br><span class="line">1) &quot;a1&quot;</span><br><span class="line">2) &quot;b1&quot;</span><br><span class="line">127.0.0.1:6379&gt; sdiff u2 u1</span><br><span class="line">1) &quot;w1&quot;</span><br><span class="line">127.0.0.1:6379&gt; sdiffstore u3 u1 u2</span><br><span class="line">(integer) 2</span><br><span class="line">127.0.0.1:6379&gt; smembers u3</span><br><span class="line">1) &quot;a1&quot;</span><br><span class="line">2) &quot;b1&quot;</span><br><span class="line">127.0.0.1:6379&gt; smove u2 u1 w1</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; smembers u1</span><br><span class="line">1) &quot;a1&quot;</span><br><span class="line">2) &quot;s1&quot;</span><br><span class="line">3) &quot;w1&quot;</span><br><span class="line">4) &quot;b1&quot;</span><br><span class="line">127.0.0.1:6379&gt; smembers u2</span><br><span class="line">1) &quot;s1&quot;</span><br></pre></td></tr></table></figure>

<h1 id="Set-类型应用场景"><a href="#Set-类型应用场景" class="headerlink" title="Set 类型应用场景"></a>Set 类型应用场景</h1><h1 id="权限校验"><a href="#权限校验" class="headerlink" title="权限校验"></a>权限校验</h1><p>集团公司共具有 12000 名员工，内部 OA 系统中具有 700 多个角色，3000 多个业务操作，23000 多种数据，每位员工具有一个或多个角色，可以快速进行业务操作的权限校验</p>
<p><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/obsition/20240202153140.png"></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379&gt; sadd rid:001 getall</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; sadd rid:001 getById</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; sadd rid:002 getall</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; sadd rid:002 getCount </span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; sadd rid:002 insert</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; sunionstore uid:007  rid:001 rid:002</span><br><span class="line">(integer) 4</span><br><span class="line">127.0.0.1:6379&gt; smembers uid:007</span><br><span class="line">1) &quot;insert&quot;</span><br><span class="line">2) &quot;getById&quot;</span><br><span class="line">3) &quot;getCount&quot;</span><br><span class="line">4) &quot;getall&quot;</span><br><span class="line">127.0.0.1:6379&gt; sismember uid:007 insert</span><br><span class="line">(integer) 1 </span><br></pre></td></tr></table></figure>

<h1 id="sorted-set"><a href="#sorted-set" class="headerlink" title="sorted_set"></a>sorted_set</h1><p><strong>sorted_set 操作</strong></p>
<p>新的存储需求：数据排序有利于数据的有效展示，需要提供一种可以根据自身特征进行排序的方式</p>
<p>需要的存储结构：新的存储模型，可以保存可排序的数据</p>
<p>sorted_set 类型：在 set 的存储结构基础上添加可排序字段</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br></pre></td><td class="code"><pre><span class="line">sorted_set 类型数据的基本操作</span><br><span class="line">添加数据</span><br><span class="line">zadd key score1 member1 [score2 member2]</span><br><span class="line">获取全部数据</span><br><span class="line">zrange key start stop [WITHSCORES]</span><br><span class="line">zrevrange key start stop [WITHSCORES]</span><br><span class="line">删除数据</span><br><span class="line">zrem key member [member ...]</span><br><span class="line">按条件获取数据</span><br><span class="line">zrangebyscore key min max [WITHSCORES] [LIMIT]</span><br><span class="line">zrevrangebyscore key max min [WITHSCORES]</span><br><span class="line">条件删除数据</span><br><span class="line">zremrangebyrank key start stop</span><br><span class="line">zremrangebyscore key min max</span><br><span class="line">注意：</span><br><span class="line">min与max用于限定搜索查询的条件</span><br><span class="line">start与stop用于限定查询范围，作用于索引，表示开始和结束索引</span><br><span class="line">offset与count用于限定查询范围，作用于查询结果，表示开始位置和数据总量</span><br><span class="line"> 获取集合数据总量</span><br><span class="line"> zcard key</span><br><span class="line">zcount key min max</span><br><span class="line">集合交、并操作</span><br><span class="line">zinterstore destination numkeys key [key ...]</span><br><span class="line">zunionstore destination numkeys key [key ...]</span><br><span class="line"></span><br><span class="line">127.0.0.1:6379&gt; zadd scores 94 zs</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; zadd scores 100 ls</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; zadd scores 60 ww</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; zadd scores 47 zl</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; zrange scores 0 -1</span><br><span class="line">1) &quot;zl&quot;</span><br><span class="line">2) &quot;ww&quot;</span><br><span class="line">3) &quot;zs&quot;</span><br><span class="line">4) &quot;ls&quot;</span><br><span class="line">127.0.0.1:6379&gt; zrange scores 0 -1 withscores</span><br><span class="line">1) &quot;zl&quot;</span><br><span class="line">2) &quot;47&quot;</span><br><span class="line">3) &quot;ww&quot;</span><br><span class="line">4) &quot;60&quot;</span><br><span class="line">5) &quot;zs&quot;</span><br><span class="line">6) &quot;94&quot;</span><br><span class="line">7) &quot;ls&quot;</span><br><span class="line">8) &quot;100&quot;</span><br><span class="line">127.0.0.1:6379&gt; zrevrange scores 0 -1 withscores</span><br><span class="line">1) &quot;ls&quot;</span><br><span class="line">2) &quot;100&quot;</span><br><span class="line">3) &quot;zs&quot;</span><br><span class="line">4) &quot;94&quot;</span><br><span class="line">5) &quot;ww&quot;</span><br><span class="line">6) &quot;60&quot;</span><br><span class="line">7) &quot;zl&quot;</span><br><span class="line">8) &quot;47&quot;</span><br><span class="line">127.0.0.1:6379&gt; zrem scores ls</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; zrevrange scores 0 -1 withscores</span><br><span class="line">1) &quot;zs&quot;</span><br><span class="line">2) &quot;94&quot;</span><br><span class="line">3) &quot;ww&quot;</span><br><span class="line">4) &quot;60&quot;</span><br><span class="line">5) &quot;zl&quot;</span><br><span class="line">6) &quot;47&quot;</span><br><span class="line"></span><br><span class="line">127.0.0.1:6379&gt; zrange scores 0 -1 withscores</span><br><span class="line"> 1) &quot;wangwu&quot;</span><br><span class="line"> 2) &quot;45&quot;</span><br><span class="line"> 3) &quot;zhangsan&quot;</span><br><span class="line"> 4) &quot;67&quot;</span><br><span class="line"> 5) &quot;zhouqi&quot;</span><br><span class="line"> 6) &quot;71&quot;</span><br><span class="line"> 7) &quot;qianba&quot;</span><br><span class="line"> 8) &quot;92&quot;</span><br><span class="line"> 9) &quot;lisi&quot;</span><br><span class="line">10) &quot;99&quot;</span><br><span class="line">11) &quot;zhangliu&quot;</span><br><span class="line">12) &quot;100&quot;</span><br><span class="line">127.0.0.1:6379&gt; zrangebyscore scores 50 80 withscores</span><br><span class="line">1) &quot;zhangsan&quot;</span><br><span class="line">2) &quot;67&quot;</span><br><span class="line">3) &quot;zhouqi&quot;</span><br><span class="line">4) &quot;71&quot;</span><br><span class="line">127.0.0.1:6379&gt; zrevrangebyscore scores 80 50 withscores</span><br><span class="line">1) &quot;zhouqi&quot;</span><br><span class="line">2) &quot;71&quot;</span><br><span class="line">3) &quot;zhangsan&quot;</span><br><span class="line">4) &quot;67&quot;</span><br><span class="line">127.0.0.1:6379&gt; zrangebyscore scores 50 99 withscores</span><br><span class="line">1) &quot;zhangsan&quot;</span><br><span class="line">2) &quot;67&quot;</span><br><span class="line">3) &quot;zhouqi&quot;</span><br><span class="line">4) &quot;71&quot;</span><br><span class="line">5) &quot;qianba&quot;</span><br><span class="line">6) &quot;92&quot;</span><br><span class="line">7) &quot;lisi&quot;</span><br><span class="line">8) &quot;99&quot;</span><br><span class="line">127.0.0.1:6379&gt; zrangebyscore scores 50 99 withscores limit 0 3</span><br><span class="line">1) &quot;zhangsan&quot;</span><br><span class="line">2) &quot;67&quot;</span><br><span class="line">3) &quot;zhouqi&quot;</span><br><span class="line">4) &quot;71&quot;</span><br><span class="line">5) &quot;qianba&quot;</span><br><span class="line">6) &quot;92&quot;</span><br><span class="line">127.0.0.1:6379&gt; zrangebyscore scores 50 99 withscores limit 0 1</span><br><span class="line">1) &quot;zhangsan&quot;</span><br><span class="line">2) &quot;67&quot;</span><br><span class="line">127.0.0.1:6379&gt; zremrangebyscore scores 50 70</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; zrange scores 0 -1 withscores</span><br><span class="line"> 1) &quot;wangwu&quot;</span><br><span class="line"> 2) &quot;45&quot;</span><br><span class="line"> 3) &quot;zhouqi&quot;</span><br><span class="line"> 4) &quot;71&quot;</span><br><span class="line"> 5) &quot;qianba&quot;</span><br><span class="line"> 6) &quot;92&quot;</span><br><span class="line"> 7) &quot;lisi&quot;</span><br><span class="line"> 8) &quot;99&quot;</span><br><span class="line"> 9) &quot;zhangliu&quot;</span><br><span class="line">10) &quot;100&quot;</span><br><span class="line">127.0.0.1:6379&gt; zremrangebyrank scores 0 1</span><br><span class="line">(integer) 2</span><br><span class="line">127.0.0.1:6379&gt; zrange scores 0 -1 withscores</span><br><span class="line">1) &quot;qianba&quot;</span><br><span class="line">2) &quot;92&quot;</span><br><span class="line">3) &quot;lisi&quot;</span><br><span class="line">4) &quot;99&quot;</span><br><span class="line">5) &quot;zhangliu&quot;</span><br><span class="line">6) &quot;100&quot;</span><br><span class="line">127.0.0.1:6379&gt; zcard scores</span><br><span class="line">(integer) 3</span><br><span class="line">127.0.0.1:6379&gt; zcount scores 99 200</span><br><span class="line">(integer) 2</span><br><span class="line">127.0.0.1:6379&gt; zadd s1 50 aa 60 bb 70 cc</span><br><span class="line">(integer) 3</span><br><span class="line">127.0.0.1:6379&gt; zadd s2 60 aa 40 bb 90 aa</span><br><span class="line">(integer) 2</span><br><span class="line">127.0.0.1:6379&gt; zadd s3 70 aa 20 bb 100 dd</span><br><span class="line">(integer) 3</span><br><span class="line">127.0.0.1:6379&gt; zinterstore sss 3 s1 s2 s3   # 求和</span><br><span class="line">(integer) 2</span><br><span class="line">127.0.0.1:6379&gt; zrange sss 0 -1 withscores</span><br><span class="line">1) &quot;bb&quot;</span><br><span class="line">2) &quot;120&quot;</span><br><span class="line">3) &quot;aa&quot;</span><br><span class="line">4) &quot;210&quot;</span><br><span class="line">127.0.0.1:6379&gt; zinterstore ssss 3 s1 s2 s3 aggregate max  # 求最大值</span><br><span class="line">(integer) 2</span><br><span class="line">127.0.0.1:6379&gt; zrange ssss 0 -1 withscores</span><br><span class="line">1) &quot;bb&quot;</span><br><span class="line">2) &quot;60&quot;</span><br><span class="line">3) &quot;aa&quot;</span><br><span class="line">4) &quot;90&quot;</span><br><span class="line">127.0.0.1:6379&gt; help zunionstore</span><br><span class="line"></span><br><span class="line">  ZUNIONSTORE destination numkeys key [key ...] [WEIGHTS weight] [AGGREGATE SUM|MIN|MAX]</span><br><span class="line">  summary: Add multiple sorted sets and store the resulting sorted set in a new key</span><br><span class="line">  since: 2.0.0</span><br><span class="line">  group: sorted_set</span><br><span class="line"></span><br><span class="line">127.0.0.1:6379&gt; help zinterstore</span><br><span class="line"></span><br><span class="line">  ZINTERSTORE destination numkeys key [key ...] [WEIGHTS weight] [AGGREGATE SUM|MIN|MAX]</span><br><span class="line">  summary: Intersect multiple sorted sets and store the resulting sorted set in a new key</span><br><span class="line">  since: 2.0.0</span><br><span class="line">  group: sorted_set</span><br><span class="line"></span><br><span class="line">sorted_set 类型数据的扩展操作</span><br><span class="line">获取数据对应的索引（排名）</span><br><span class="line">zrank key member</span><br><span class="line">zrevrank key member</span><br><span class="line">score值获取与修改</span><br><span class="line">zscore key member</span><br><span class="line">zincrby key increment member</span><br><span class="line">Tips 11：</span><br><span class="line">redis 应用于计数器组合排序功能对应的排名</span><br><span class="line"></span><br><span class="line">127.0.0.1:6379&gt; zadd movies 143 aa 97 bb 201 cc</span><br><span class="line">(integer) 3</span><br><span class="line">127.0.0.1:6379&gt; zrank movies bb</span><br><span class="line">(integer) 0</span><br><span class="line">127.0.0.1:6379&gt; zrevrank movies bb</span><br><span class="line">(integer) 2</span><br><span class="line">127.0.0.1:6379&gt; zscore movies aa</span><br><span class="line">&quot;143&quot;</span><br><span class="line">127.0.0.1:6379&gt; zincrby movies 1 aa</span><br><span class="line">&quot;144&quot;</span><br><span class="line">127.0.0.1:6379&gt; zscore movies aa</span><br><span class="line">&quot;144&quot;</span><br><span class="line"></span><br><span class="line">sorted_set 类型数据操作的注意事项</span><br><span class="line">score保存的数据存储空间是64位，如果是整数范围是-9007199254740992~9007199254740992</span><br><span class="line">score保存的数据也可以是一个双精度的double值，基于双精度浮点数的特征，可能会丢失精度，使用时</span><br><span class="line">候要慎重</span><br><span class="line">sorted_set 底层存储还是基于set结构的，因此数据不能重复，如果重复添加相同的数据，score值将被反</span><br><span class="line">复覆盖，保留最后一次修改的结果 </span><br></pre></td></tr></table></figure>

<h1 id="sorted-set-类型应用场景"><a href="#sorted-set-类型应用场景" class="headerlink" title="sorted_set 类型应用场景"></a>sorted_set 类型应用场景</h1><h1 id="微信接受消息顺序控制"><a href="#微信接受消息顺序控制" class="headerlink" title="微信接受消息顺序控制"></a>微信接受消息顺序控制</h1><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">使用微信的过程中，当微信接收消息后，会默认将最近接收的消息置顶，当多个好友及关注的订阅号同时发</span><br><span class="line">送消息时，该排序会不停的进行交替。同时还可以将重要的会话设置为置顶。一旦用户离线后，再次打开微</span><br><span class="line">信时，消息该按照什么样的顺序显示？</span><br><span class="line"></span><br><span class="line">依赖list的数据具有顺序的特征对消息进行管理，将list结构作为栈使用</span><br><span class="line">对置顶与普通会话分别创建独立的list分别管理</span><br><span class="line">当某个list中接收到用户消息后，将消息发送方的id从list的一侧加入list（此处设定左侧）</span><br><span class="line">多个相同id发出的消息反复入栈会出现问题，在入栈之前无论是否具有当前id对应的消息，先删除对应id</span><br><span class="line">推送消息时先推送置顶会话list，再推送普通会话list，推送完成的list清除所有数据</span><br><span class="line">消息的数量，也就是微信用户对话数量采用计数器的思想另行记录，伴随list操作同步更新</span><br><span class="line"></span><br><span class="line">Tips 15：</span><br><span class="line">redis 应用于基于时间顺序的数据操作，而不关注具体时间</span><br></pre></td></tr></table></figure>
<p><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/obsition/20240202153155.png"></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379&gt; lrem 100 1 200</span><br><span class="line">(integer) 0</span><br><span class="line">127.0.0.1:6379&gt; lpush 100 200</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; lrem 100 1 300</span><br><span class="line">(integer) 0</span><br><span class="line">127.0.0.1:6379&gt; lpush 100 300</span><br><span class="line">(integer) 2</span><br><span class="line">127.0.0.1:6379&gt; lrem 100 1 400</span><br><span class="line">(integer) 0</span><br><span class="line">127.0.0.1:6379&gt; lpush 100 400</span><br><span class="line">(integer) 3</span><br><span class="line">127.0.0.1:6379&gt; lrem 100 1 200</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; lpush 100 200</span><br><span class="line">(integer) 3</span><br><span class="line">127.0.0.1:6379&gt; lrem 100 1 300</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; lpush 100 300</span><br><span class="line">(integer) 3</span><br><span class="line">127.0.0.1:6379&gt; lrange 100 0 -1</span><br><span class="line">1) &quot;300&quot;</span><br><span class="line">2) &quot;200&quot;</span><br><span class="line">3) &quot;400&quot;</span><br></pre></td></tr></table></figure></article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta"><i class="fas fa-circle-user fa-fw"></i>文章作者: </span><span class="post-copyright-info"><a href="https://ko25891wan.gitlab.io">十一星野</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta"><i class="fas fa-square-arrow-up-right fa-fw"></i>文章链接: </span><span class="post-copyright-info"><a href="https://ko25891wan.gitlab.io/2023/12/dd803e04aee5.html">https://ko25891wan.gitlab.io/2023/12/dd803e04aee5.html</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta"><i class="fas fa-circle-exclamation fa-fw"></i>版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://ko25891wan.gitlab.io" target="_blank">小小程序员</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/redis/">redis</a></div><div class="post_share"></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2023/12/4745b3973e37.html" title="二手交易-测试工程师"><div class="cover" style="background: var(--default-bg-color)"></div><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">二手交易-测试工程师</div></div></a></div><div class="next-post pull-right"><a href="/2023/12/522e4997d252.html" title="二手交易-客户经理"><div class="cover" style="background: var(--default-bg-color)"></div><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">二手交易-客户经理</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="https://qiniu.ko25891wan.top/%E6%97%A5%E8%AE%B0%E8%BD%AF%E4%BB%B6/%E5%A4%B4%E5%83%8F/%E7%81%B0%E5%A4%AA%E7%8B%BC.png" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">十一星野</div><div class="author-info__description">归途也还可爱</div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">120</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">4</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">22</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://gitlab.com/ko25891wan/ko25891wan.gitlab.io"><i class="fab fa-github"></i><span>关注我</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://gitlab.com/ko25891wan/ko25891wan.gitlab.io" target="_blank" title="Github"><i class="fab fa-github" style="color: #24292e;"></i></a><a class="social-icon" href="https://gitlab.com/ko25891wan/ko25891wan.gitlab.io" target="_blank" title="Gitlab"><i class="fab fa-gitlab" style="color: #24292e;"></i></a><a class="social-icon" href="mailto:ko25891wan@outlook.com" target="_blank" title="Email"><i class="fas fa-envelope" style="color: #4a7dbe;"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">This is my Blog</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#Redis-%E7%9A%84%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E4%BB%A5%E5%8F%8A%E4%BD%BF%E7%94%A8%E5%9C%BA%E6%99%AF%EF%BC%8C%E7%9C%8B%E8%BF%99%E4%B8%80%E7%AF%87%E5%B0%B1%E5%A4%9F%E5%95%A6"><span class="toc-number">1.</span> <span class="toc-text">Redis 的数据类型以及使用场景，看这一篇就够啦</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Redis-%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B"><span class="toc-number">2.</span> <span class="toc-text">Redis 数据类型</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#redis-%E6%95%B0%E6%8D%AE%E5%AD%98%E5%82%A8%E6%A0%BC%E5%BC%8F"><span class="toc-number">3.</span> <span class="toc-text">redis 数据存储格式</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#string-%E7%B1%BB%E5%9E%8B"><span class="toc-number">4.</span> <span class="toc-text">string 类型</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#string-%E6%93%8D%E4%BD%9C"><span class="toc-number">5.</span> <span class="toc-text">string 操作</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#string-%E7%B1%BB%E5%9E%8B%E5%BA%94%E7%94%A8%E5%9C%BA%E6%99%AF"><span class="toc-number">6.</span> <span class="toc-text">string 类型应用场景</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#hash"><span class="toc-number">7.</span> <span class="toc-text">hash</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#hash-%E6%93%8D%E4%BD%9C"><span class="toc-number">8.</span> <span class="toc-text">hash 操作</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Hash-%E7%B1%BB%E5%9E%8B%E5%BA%94%E7%94%A8%E5%9C%BA%E6%99%AF"><span class="toc-number">9.</span> <span class="toc-text">Hash 类型应用场景</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E7%94%B5%E5%95%86%E7%BD%91%E7%AB%99%E8%B4%AD%E7%89%A9%E8%BD%A6%E8%AE%BE%E8%AE%A1%E4%B8%8E%E5%AE%9E%E7%8E%B0"><span class="toc-number">10.</span> <span class="toc-text">电商网站购物车设计与实现</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#list"><span class="toc-number">11.</span> <span class="toc-text">list</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#list-%E6%93%8D%E4%BD%9C"><span class="toc-number">12.</span> <span class="toc-text">list 操作</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#List-%E7%B1%BB%E5%9E%8B%E5%BA%94%E7%94%A8%E5%9C%BA%E6%99%AF"><span class="toc-number">13.</span> <span class="toc-text">List 类型应用场景</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#set-%E7%B1%BB%E5%9E%8B"><span class="toc-number">14.</span> <span class="toc-text">set 类型</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#set-%E6%93%8D%E4%BD%9C"><span class="toc-number">15.</span> <span class="toc-text">set 操作</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Set-%E7%B1%BB%E5%9E%8B%E5%BA%94%E7%94%A8%E5%9C%BA%E6%99%AF"><span class="toc-number">16.</span> <span class="toc-text">Set 类型应用场景</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E6%9D%83%E9%99%90%E6%A0%A1%E9%AA%8C"><span class="toc-number">17.</span> <span class="toc-text">权限校验</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#sorted-set"><span class="toc-number">18.</span> <span class="toc-text">sorted_set</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#sorted-set-%E7%B1%BB%E5%9E%8B%E5%BA%94%E7%94%A8%E5%9C%BA%E6%99%AF"><span class="toc-number">19.</span> <span class="toc-text">sorted_set 类型应用场景</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%BE%AE%E4%BF%A1%E6%8E%A5%E5%8F%97%E6%B6%88%E6%81%AF%E9%A1%BA%E5%BA%8F%E6%8E%A7%E5%88%B6"><span class="toc-number">20.</span> <span class="toc-text">微信接受消息顺序控制</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2024/02/c60470b9a892.html" title="Java集合使用注意事项总结">Java集合使用注意事项总结</a><time datetime="2024-02-03T04:58:22.000Z" title="发表于 2024-02-03 12:58:22">2024-02-03</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2024/02/81fb7a201f29.html" title="无题">无题</a><time datetime="2024-02-03T03:15:31.812Z" title="发表于 2024-02-03 11:15:31">2024-02-03</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2024/01/5737c1ec69a9.html" title="Wed Jan 17 2024 00:00:00 GMT+0800 (中国标准时间)">Wed Jan 17 2024 00:00:00 GMT+0800 (中国标准时间)</a><time datetime="2024-01-31T06:56:16.000Z" title="发表于 2024-01-31 14:56:16">2024-01-31</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2024/01/f510db7d2f19.html" title="Spring常见面试题总结">Spring常见面试题总结</a><time datetime="2024-01-31T06:55:58.000Z" title="发表于 2024-01-31 14:55:58">2024-01-31</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2024/01/9be065e9f288.html" title="redis 面试">redis 面试</a><time datetime="2024-01-31T06:55:55.000Z" title="发表于 2024-01-31 14:55:55">2024-01-31</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2023 - 2024 By 十一星野</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="translateLink" type="button" title="简繁转换">简</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside-config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="/js/tw_cn.js"></script><script src="https://cdn.staticfile.org/fancyapps-ui/5.0.32/fancybox/fancybox.umd.min.js"></script><script src="https://cdn.staticfile.org/instant.page/5.2.0/instantpage.min.js" type="module"></script><div class="js-pjax"></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div><div id="local-search-stats-wrap"></div></div></div><div id="search-mask"></div><script src="/js/search/local-search.js"></script></div></div></body></html>